const WXAPI = require('../../wxapi/main')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    vcodeValue: false,
    msg: false,
    checkValue: 1,
    check: false,
  },

  bindPhoneInput(e) {
      this.setData({
        phone: e.detail.value,
      });
  },
  bindVcodeInput(e) {
      this.setData({
        vcodeValue: e.detail.value,
      });
  },
  checkTelephone(telephone) {
    var reg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!reg.test(telephone)) {
      return false;
    } else {
      return true;
    }
  },
  sendCode() {
    let that = this
    let _phone = that.data.phone
    console.log('_phone ', _phone)
    if (!that.checkTelephone(_phone)) {
      wx.showToast({
        title: '手机号验证错误',
        icon: 'none'
      })
      return
    }
    console.log('success')
    WXAPI.sendVCode({phone: _phone})
    .then(res => {
      console.log('res ', res)
      if (res.code === 0) {
        wx.showToast({
          title: '发送成功',
          icon: 'success'
        })
      } else {
        wx.showToast({
          title: '发送异常',
          icon: res.message
        })
      }
    })
  },
  checkStatus() {
    if (!this.data.check) {
      this.setData({
        msg: true,
      });
      return
    }
    console.log('exec check...')
    const that = this;
    setTimeout(() => {
      that.setData({
        msg: false,
      });
    }, 320);
    let _phone = that.data.phone
    let _vsCode = that.data.vcodeValue
    // 验证通过
    console.log('create user ', _phone, _vsCode)
    wx.login({
      success: function(loginRes) {
        if (loginRes.code) {
          wx.request({
            url: app.globalApi.loginUrl,
            data: {
              code: loginRes.code
            },
            success: function(res) {
              console.log('login success', res);         
              let resData = res.data     
              if (resData.result == 0) {
                let _resData =  resData.data
                // app.globalData.partUserInfo = _resData;
                let {session_key, skey, openid} = _resData
                // 调用手机号
                let _inviteCode = wx.getStorageSync('inviteCode')?wx.getStorageSync('inviteCode'):''
                console.log('_inviteCode ', _inviteCode)
                let _initPhonePost = {sessionKey:session_key, skey, openid, phone:_phone, inviteCode: _inviteCode}
                console.log('request post ', _initPhonePost)
                WXAPI.initialUser(_initPhonePost).then(res2 => { // 客户端获取手机号
                  console.log('init user ret ', res2)
                  if (res2.result == 0) {
                    wx.setStorageSync('partUserInfo', res2.data)
                    wx.setStorageSync('loginFlag', res2.data.skey);                   
                    if (_inviteCode) {
                      // 如果有推荐的话，增加积分1
                      WXAPI.increaseScore({inviteCode: _inviteCode}).then(res3 => {
                        console.log('increaseScore ', res3)
                      })
                    }
                    // 回到原来的地方放
                    app.navigateToLogin = false
                    // wx.navigateBack();
                    wx.switchTab({
                      url: '/pages/index/index',
                    })
                  }
                })              
              } else {
                app.showInfo(res.errmsg)
              }
            },
          })
        }
      },
      fail: function() {
        // 获取临时code失败
        that.showInfo('登录失败')
        console.log('调用wx.login获取code失败');
      }
    })
  },
  checkboxChange(e) {
    if (e.detail.value.includes('1')) {
      this.setData({
        check: true,
      });
    } else {
      this.setData({
        check: false,
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    // 回到原来的地方放
    app.navigateToLogin = false
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})